
// Enum defines how to represent information
var timeShowType = { system : 0, custom : 1, other : 2 };

// Array containing weekday names
 var weekDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday");

// Current data representation type
var viewType;   

var clockRefreshInterval = 207;

/**
 * Setting default data on application load
 */

function init() {

    // Setting information layout to custom
    viewType = timeShowType.custom;
    // Setting checkboxes marks
    document.getElementById('dysplay_type').custom_output.checked = true;
    document.getElementById('params').Date.checked = true;
    document.getElementById('params').Time.checked = true;
        
    // Setting handlers for main menu items
    //standardData.onSelect = setStandard;
    //custom.onSelect = setCustom;

    // Start timer for time displaying
    setInterval( 'startTime()', clockRefreshInterval );
    startTime();
}

/**
 * Set data display type to system
 */
function setStandard() {
    document.getElementById('dysplay_type').system_output.checked = true;
    document.getElementById('dysplay_type').custom_output.checked = false;
    document.getElementById('params').style.display = "none";
    viewType = timeShowType.system;
}

/**
 * Set data display type to custom
 */
function setCustom() {
    document.getElementById('dysplay_type').system_output.checked = false;
    document.getElementById('dysplay_type').custom_output.checked = true;
    document.getElementById('params').style.display = "block";
    viewType = timeShowType.custom;
}

/**
 * Function repeatedly called for updating current time info
 */
function startTime() {
    // Get current date info
    var today = new Date();
    // Laying out data
    if(viewType == timeShowType.system) {
        // Default layout of data
        document.getElementById('txt').innerHTML = Date();
    } else if (viewType == timeShowType.custom) {
        var result = "";
        // Add date to result string
        if( document.getElementById('params').Date.checked == true ) {
            result += today.getFullYear() + "/" + (today.getMonth()+1) + "/" + 
                    today.getDate() + " ";
        }
        // Add week day to result string
        if(document.getElementById('params').Weekday.checked == true) {
            result += weekDays[today.getDay()] + " ";
        }
        // Add time to result string
        if(document.getElementById('params').Time.checked == true) {
            var h=today.getHours();
            var m=today.getMinutes();
            var s=today.getSeconds();
            // Add a zero in front of numbers<10
            m=checkTime(m);
            s=checkTime(s);
            result += h + ":" + m + ":" + s + ".";
        }
        // Add milliseconds to result string
        if(document.getElementById('params').MSeconds.checked == true) {
            result += today.getMilliseconds();
        }
        // receive element 'txt' in DOM and set its html text
        document.getElementById('txt').innerHTML = result;
    }
}

/**
 * If time is represented by one digit, add '0' in front of it.
 * @param i string to convert to two digits
 * @return string representing number in two or more digits
 */
function checkTime(i) {
    if(i < 10) {
        i = "0" + i;
    }
    return i;
}
